package com.seg.web.controller.wms;

import com.seg.common.annotation.Log;
import com.seg.common.core.controller.BaseController;
import com.seg.common.core.domain.AjaxResult;
import com.seg.common.core.domain.entity.SysRole;
import com.seg.common.core.page.TableDataInfo;
import com.seg.common.enums.BusinessType;
import com.seg.common.utils.poi.ExcelUtil;
import com.seg.framework.web.domain.server.Sys;
import com.seg.system.domain.TReturnWarehouse;
import com.seg.system.service.ITReturnWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 备件退库Controller
 *
 * @author seg
 * @date 2023-07-04
 */
@RestController
@RequestMapping("/wms/returnn")
public class TReturnWarehouseController extends BaseController {
    @Autowired
    private ITReturnWarehouseService tReturnWarehouseService;

    /**
     * 查询备件退库列表
     */
    @PreAuthorize("@ss.hasPermi('wms:warehouse:history:returnn')")
    @GetMapping("/list")
    public TableDataInfo list(TReturnWarehouse tReturnWarehouse) {
        startPage();
        List<TReturnWarehouse> list = tReturnWarehouseService.selectTReturnWarehouseList(tReturnWarehouse);
        return getDataTable(list);
    }

    /**
     * 导出备件退库列表
     */
    @PreAuthorize("@ss.hasPermi('wms:warehouse:returnn:export')")
    @Log(title = "备件退库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TReturnWarehouse tReturnWarehouse) {
        List<TReturnWarehouse> list = tReturnWarehouseService.selectTReturnWarehouseList(tReturnWarehouse);
        ExcelUtil<TReturnWarehouse> util = new ExcelUtil<>(TReturnWarehouse.class);
        util.exportExcel(response, list, "备件退库数据");
    }

    /**
     * 获取备件退库详细信息
     */
    @PreAuthorize("@ss.hasPermi('wms:returnn:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tReturnWarehouseService.selectTReturnWarehouseById(id));
    }

    /**
     * 新增备件退库
     */
    @PreAuthorize("@ss.hasPermi('wms:warehouse:returnn:add')")
    @Log(title = "备件退库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TReturnWarehouse tReturnWarehouse) {
        String operName = getUsername();
        int flag = tReturnWarehouseService.insertTReturnWarehouse(tReturnWarehouse, operName);
        if (flag == -1) {
            return toAjax1(flag, "备件不存在");
        }
        return toAjax1(flag, "退库成功");
    }


    @Log(title = "备件退库申请", businessType = BusinessType.INSERT)
    @PostMapping("/part_return")
    public AjaxResult partReturn(@RequestBody Long[] ids) {
        List<SysRole> roles = getLoginUser().getSysUser().getRoles();
        boolean hasPer = false;
        for(SysRole role : roles){
            Long roleId = role.getRoleId();
            System.out.println("测试李一帆"+roleId);
            if(roleId == 1L || roleId == 2L || roleId == 3L || roleId == 4 || roleId == 5){
                hasPer = true;
                break;
            }
        }
        if(hasPer){
            String operName = getUsername();
            int flag = tReturnWarehouseService.tReturnWarehouse(ids, operName);
            if(flag!=1)
                return toAjax1(flag, "退库申请失败");
            return toAjax1(flag, "退库申请成功");
        }else {
            return toAjax1(-3,"没有该权限!");
        }

    }

    /**
     * @description 备件退库：一级审批
     * @param id
     * @return
     */
    @Log(title = "备件退库：一级审批", businessType = BusinessType.INSERT)
    @GetMapping("/part_return_approval1/{id}")
    public AjaxResult partReturnApproval1(@PathVariable("id") Long id) {
        List<SysRole> roles = getLoginUser().getSysUser().getRoles();
        boolean hasPer = false;
        for(SysRole role : roles){
            Long roleId = role.getRoleId();
            if(roleId == 1L || roleId == 2L || roleId == 4 || roleId ==5){
                hasPer = true;
                break;
            }
        }
        if(hasPer){
            String operName = getUsername();
            Long [] ids= {id};
            int flag = tReturnWarehouseService.partReturnApproval1(ids, operName);
            if (flag != 1) {
                return toAjax1(-3,"退库失败!");
            }
            return toAjax1(flag, "退库成功");
        }else {
            return toAjax1(-3,"没有该权限!");
        }
    }

    /**
     * @description 备件退库：二级审批
     * @param id
     * @return
     */
    @Log(title = "备件退库：二级审批", businessType = BusinessType.INSERT)
    @GetMapping("/part_return_approval2/{id}")
    public AjaxResult partReturnApproval2(@PathVariable("id") Long id) {
        List<SysRole> roles = getLoginUser().getSysUser().getRoles();
        boolean hasPer = false;
        for(SysRole role : roles){
            Long roleId = role.getRoleId();
            if(roleId == 1L || roleId == 2L){
                hasPer = true;
                break;
            }
        }
        if(hasPer){
            String operName = getUsername();
            Long [] ids= {id};
            int flag = tReturnWarehouseService.partReturnApproval2(ids, operName);
            if (flag !=1) {
                return toAjax1(flag, "退库失败");
            }
            return toAjax1(flag, "退库成功");
        }else {
            return toAjax1(-3,"没有该权限!");
        }
    }

    /**
     * 修改备件退库
     */
    @PreAuthorize("@ss.hasPermi('wms:returnn:edit')")
    @Log(title = "备件退库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TReturnWarehouse tReturnWarehouse) {
        return toAjax(tReturnWarehouseService.updateTReturnWarehouse(tReturnWarehouse));
    }

    /**
     * 删除备件退库
     */
    @PreAuthorize("@ss.hasPermi('wms:warehouse:returnn:remove')")
    @Log(title = "备件退库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tReturnWarehouseService.deleteTReturnWarehouseByIds(ids));
    }
}
